package com.cskaoyan.gateway.controller.shopping;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.IProductDetailService;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <h1> ProductController </h1>
 * Created by GaoJia
 * Date  2021/11/1 21:23
 */
@Slf4j
@RestController
@RequestMapping("/shopping")
public class ProductController {

    // 使用 dubbo 的注解 注入服务
    @Reference(timeout = 3000, retries = 0, check = false)
    IProductService iProductService;



    @GetMapping("product/{productId}")// 通过@PathVariable获取路径中的参数
    @Anoymous// 加入 Anoymous 代表放行
    public ResponseData getProductDetail(@PathVariable Long productId){

        // 创建请求
        ProductDetailRequest request = new ProductDetailRequest();
        request.setId(productId);
        // 调用服务详细的业务方法 首先进行数据的 校验  数据的增删改查 （这里面进行 异常捕获并且处理）
        ProductDetailResponse productDetail = iProductService.getProductDetail(request);

//        判断成功就 返回正确的数据 失败就返回 对应的状态码
        if (ShoppingRetCode.SUCCESS.getCode().equals(productDetail.getCode())) {
            return new ResponseUtil().setData(productDetail.getProductDetailDto());
        }

        return new ResponseUtil().setErrorMsg(productDetail.getMsg());
    }

    /**
     * <h2> 分页查询商品列表 </h2>
     * Created by GaoJia
     *
     */
    @GetMapping("goods")
    @Anoymous //放行
    public ResponseData getAllProduct(AllProductRequest request) {

        // 调用服务
        AllProductResponse response = iProductService.getAllProduct(request);

        // 判断状态码是否正常
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 返回Response Data
//            return new ResponseUtil().setData(response.getData());
            return new ResponseUtil().setData(response);
        }

        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    /**
     * <h2> 查询推荐商品 </h2>
     * Created by GaoJia
     *
     */
    @GetMapping("recommend")
    @Anoymous //放行
    public ResponseData getRecommendGoods() {

        // 首先 创建 request (没有请求参数)


        RecommendResponse recommendGoods = iProductService.getRecommendGoods(); // 调用服务
        if (ShoppingRetCode.SUCCESS.getCode().equals(recommendGoods.getCode())) { // 判断状态码是否正常
            // 返回Response Data
            return new ResponseUtil().setData(recommendGoods.getPanelContentItemDtos());
        }

        return new ResponseUtil().setErrorMsg(recommendGoods.getMsg());
    }
}
